package com.itheima.controller;

import com.itheima.MessageConstant;
import com.itheima.pojo.File;
import com.itheima.service.FileService;
import com.itheima.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 图片服务相关接口
 */
@RestController
@RequestMapping("/file")
@CrossOrigin
@Api(tags = "图片服务相关接口")
public class FileController {

    @Autowired
    private FileService fileService;

    @PostMapping("/pictures")
    @ApiOperation("图片上传七牛云")
    public String pictures(HttpServletRequest request, List<MultipartFile> filedatas){
        try {
            List<File> files = fileService.pictures(request, filedatas);
            return ResultUtil.result(200,files);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.PICTURE_LODE_FAIL);
        }
    }

    @GetMapping("/getPicture")
    @ApiOperation("根据ids获取图片信息")
    public String getPicture(@RequestParam String fileIds,@RequestParam String code){
        try {
            List<File> files = fileService.getPicture(fileIds, code);
            return ResultUtil.result(200,files);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401,MessageConstant.PICTURE_FIND_FAIL);
        }
    }
}
